package com.mvp.shq.washer.ui.shop.product;

import android.support.v4.content.ContextCompat;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Spinner;
import android.widget.TextView;

import com.github.clans.fab.FloatingActionMenu;
import com.jaeger.library.StatusBarUtil;
import com.mvp.shq.washer.R;
import com.mvp.shq.washer.adapter.ProductAdapter;
import com.mvp.shq.washer.adapter.ShopSpecAdapter;
import com.mvp.shq.washer.adapter.SpinnerAdapter;
import com.mvp.shq.washer.base.BaseActivity;
import com.mvp.shq.washer.entity.ShopSortType;
import com.mvp.shq.washer.entity.shop.Product;
import com.mvp.shq.washer.entity.shop.ShopAttr;
import com.mvp.shq.washer.entity.shop.ShopProductJump;
import com.mvp.shq.washer.entity.shop.ShopSpec;
import com.mvp.shq.washer.ui.shop.productDetail.ProductDetailActivity;
import com.mvp.shq.washer.utils.DataDictionary;
import com.mvp.shq.washer.utils.JumpUtil;
import com.mvp.shq.washer.utils.RecyclerViewDivider;
import com.mvp.shq.washer.utils.T;
import com.mvp.shq.washer.view.widget.ShopSelectMenu;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadmoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.logging.Logger;

import butterknife.BindView;
import butterknife.OnClick;

public class ProductActivity extends BaseActivity<Presenter, Model> implements ProductConstract.View {


    @BindView(R.id.back)
    ImageView back;
    @BindView(R.id.title)
    TextView title;
    @BindView(R.id.recyclerview)
    RecyclerView recyclerview;
    @BindView(R.id.refreshLayout)
    SmartRefreshLayout refreshLayout;
    @BindView(R.id.menu)
    FloatingActionMenu menu;
    @BindView(R.id.main_sp1)
    Spinner spinner;
    @BindView(R.id.tv_sales_volume)
    TextView tvSalesVolume;
    @BindView(R.id.tv_screen)
    TextView tvScreen;
    @BindView(R.id.ll_screen)
    LinearLayout llScreen;
    private int spinnerIndex = 0;
    private List<ShopSortType> types;
    private SpinnerAdapter spinnerAdapter;
    private ShopProductJump params;
    private ShopSelectMenu specMenu;
    private List<ShopSpec> specsData;
    private List<Product> products;
    private ProductAdapter adapter;
    private String orderType;
    private String useIntegral;
    private String attrIds;
    private String sortType;
    private int pageNo = 1;
    private int pageSize = 10;
    private boolean hasMoreData = true;
    private boolean isLoadMore = false;


    @Override
    public int getLayoutId() {
        return R.layout.activity_product;
    }

    @Override
    public void initView() {
        StatusBarUtil.setColor(this, ContextCompat.getColor(context, R.color.money), 9);
        params = (ShopProductJump)getIntent().getExtras().getSerializable(ShopProductJump.KEY);
        title.setText(params.getTitle());
        recyclerview.setLayoutManager(new LinearLayoutManager(context));
        RecyclerViewDivider divider = new RecyclerViewDivider(context, LinearLayoutManager.HORIZONTAL, 2, ContextCompat.getColor(context, R.color.shop_divison_bg));
        recyclerview.addItemDecoration(divider);
        products = new ArrayList<>();
        adapter = new ProductAdapter(context,products);
        adapter.setListener(new ProductAdapter.OnProductItemClickListener() {
            @Override
            public void onClick(int position) {
                JumpUtil.toProductDetail(context,products.get(position).getId(), ProductDetailActivity.PRODUCT_TYPE_SKU);
            }
        });
        recyclerview.setAdapter(adapter);
        refreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                pageNo = 1;
                isLoadMore = false;
                hasMoreData = true;
                loadData();
            }
        });
        refreshLayout.setOnLoadmoreListener(new OnLoadmoreListener() {
            @Override
            public void onLoadmore(RefreshLayout refreshlayout) {
                pageNo ++;
                isLoadMore = true;
                if (hasMoreData){
                    loadData();
                }else {
                    refreshLayout.finishLoadmore();
                }
            }
        });
        initSpinner();
        sortType = DataDictionary.SHOP_SORT_ALL;
        refreshLayout.autoRefresh();
        if (params.getType() == ShopProductJump.PRODUCT_TYPE_CATEGORY){
            presenter.getSpecByCategoryId(params.getCategoryId());
            llScreen.setVisibility(View.VISIBLE);
        }else {
            llScreen.setVisibility(View.GONE);
        }
    }
    private void loadData(){
        Map<String,String> map = new HashMap<String, String>();
        if (params.getType() == ShopProductJump.PRODUCT_TYPE_CATEGORY){
            map.put("categoryId",params.getCategoryId());
        }
        if (params.getType() == ShopProductJump.PRODUCT_TYPE_INTEGRAL){
            map.put("useIntegral","1");
        }
        if (sortType.equals(DataDictionary.SHOP_SORT_ALL)){
            //综合排序
            map.put("","");
        }else if (sortType.equals(DataDictionary.SHOP_SORT_INTEGORY)){
            //按积分排序
            map.put("sort",sortType);
            map.put("order",orderType);
        }else if (sortType.equals(DataDictionary.SHOP_SORT_CASH)){
            //价格排序
            map.put("sort",sortType);
            map.put("order",orderType);
        }else if (sortType.equals(DataDictionary.SHOP_SORT_VOLUME_NUM)){
            //销量
            map.put("sort",sortType);
        }else if (sortType.equals(DataDictionary.SHOP_SORT_SCREEN)){
            //筛选
            map.put("attrIds",attrIds);
        }
        presenter.getSkuList(map,pageNo,pageSize);
    }

    private void initSpinner(){
        types = new ArrayList<>();
        ShopSortType type = new ShopSortType("综合排序",true,"","");
        types.add(type);
        ShopSortType type1 = new ShopSortType("积分从低到高",false, DataDictionary.SHOP_SORT_INTEGORY,DataDictionary.ORDER_ASC);
        types.add(type1);
        ShopSortType type2 = new ShopSortType("积分从高到低",false,DataDictionary.SHOP_SORT_INTEGORY,DataDictionary.ORDER_DESC);
        types.add(type2);
        ShopSortType type3 = new ShopSortType("价格从低到高",false,DataDictionary.SHOP_SORT_CASH,DataDictionary.ORDER_ASC);
        types.add(type3);
        ShopSortType type4 = new ShopSortType("价格从高到低",false,DataDictionary.SHOP_SORT_CASH,DataDictionary.ORDER_DESC);
        types.add(type4);
        spinnerIndex = 0;
        spinnerAdapter = new SpinnerAdapter(context,types);
        spinner.setAdapter(spinnerAdapter);
        spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                //综合排序
                tvSalesVolume.setTextColor(ContextCompat.getColor(context,R.color.shop_text_black));
                tvScreen.setTextColor(ContextCompat.getColor(context,R.color.shop_text_black));
                types.get(spinnerIndex).setChoose(false);
                spinnerIndex = position;
                types.get(position).setChoose(true);
                spinnerAdapter.notifyDataSetChanged();
                if (position == 0){
                    //综合排序
                    useIntegral = null;
                    sortType = DataDictionary.SHOP_SORT_ALL;
                }else {
                    sortType = types.get(position).getKey();
                    orderType = types.get(position).getOrderType();
                }
                refreshLayout.autoRefresh();
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });
    }



    @OnClick({R.id.back,R.id.menu_item, R.id.menu_item1, R.id.menu_item2,R.id.ll_screen,R.id.ll_sales_volume})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.back:
                finish();
                break;
            case R.id.menu_item:
                //首页
                JumpUtil.toMall(context);
                menu.close(true);
                break;
            case R.id.menu_item1:
                //购物车
                JumpUtil.toShopCar(context);
                break;
            case R.id.menu_item2:
                //个人中心
                JumpUtil.toShopUserCenter(context);
                menu.close(true);
                break;
            case R.id.ll_screen:
                //筛选
                if (specsData == null){
                    return;
                }
                tvSalesVolume.setTextColor(ContextCompat.getColor(context,R.color.shop_text_black));
                tvScreen.setTextColor(ContextCompat.getColor(context,R.color.money));
                types.get(spinnerIndex).setChoose(false);
                spinnerAdapter.notifyDataSetChanged();
                WindowManager.LayoutParams lp = getWindow().getAttributes();
                lp.alpha = 0.7f;
                getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
                getWindow().setAttributes(lp);
                if (specMenu == null){
                    specMenu = new ShopSelectMenu(context, this, specsData, new ShopSelectMenu.OnConfirmClickListener() {
                        @Override
                        public void onClick(String content) {
                            attrIds = content;
                            refreshLayout.autoRefresh();
                        }
                    });
                }
                sortType = DataDictionary.SHOP_SORT_SCREEN;
                specMenu.showAtLocation(back,Gravity.RIGHT,0,0);
                break;
            case R.id.ll_sales_volume:
                //点击销量优先
                tvSalesVolume.setTextColor(ContextCompat.getColor(context,R.color.money));
                tvScreen.setTextColor(ContextCompat.getColor(context,R.color.shop_text_black));
                types.get(spinnerIndex).setChoose(false);
                spinnerAdapter.notifyDataSetChanged();
                sortType = DataDictionary.SHOP_SORT_VOLUME_NUM;
                refreshLayout.autoRefresh();
                break;
        }
    }

    @Override
    public void getSpecS(List<ShopSpec> list) {
        specsData = list;
    }

    @Override
    public void getSkuList(List<Product> list) {
        if (list == null){
            refreshLayout.finishRefresh();
            refreshLayout.finishLoadmore();
            return;
        }
        if (list.size() < pageSize){
            hasMoreData  = false;
        }
        if(!isLoadMore){
            products.clear();
        }
        products.addAll(list);
        adapter.notifyDataSetChanged();
        refreshLayout.finishRefresh();
        refreshLayout.finishLoadmore();
    }
}
